package com.will.txj.common.utils;

import lombok.SneakyThrows;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author: wen-yi;
 * @date: 2021/12/4 17:11;
 * @Description:
 */
public class MD5Util {

    /**
     * @Description: 字符串MD5加密
     * @Param:  源字符串
     * @return:  加密后的字符串
     * @Author: guohailong
     * @Date: 2018/4/12
     */
    public static String getMD5Hash(String source){
        StringBuilder sb = new StringBuilder();
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] result = md5.digest(source.getBytes());
            for (byte b : result) {
                sb.append(String.format("%02X", b)); // 10进制转16进制，X 表示以十六进制形式输出，02 表示不足两位前面补0输出
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    @SneakyThrows
    public static void main(String[] args){
//        String phone = "18782969239";
//        String chl = "C0001";
//        String key = "71e154e9b1084ccaa487349c169198d0";
//        String source = chl+phone+key;
//        System.out.println(getMD5Hash(source));

        System.out.println(getMD5Hash("admin"));
        // 21232F297A57A5A743894A0E4A801FC3
        MessageDigest md5 = MessageDigest.getInstance("md5");
        byte[] digest = md5.digest("admin".getBytes(StandardCharsets.UTF_8));
        String string = Base64.getEncoder().encodeToString(digest);
        System.out.println(string);
        // ISMvKXpXpadDiUoOSoAfww==
    }
}
